/*
 * @(#)MDIFrame.java	1.0 Apr 27, 2008
 *
 *	The MIT License
 *
 *	Copyright (c) 2008 Malachi de AElfweald <malachid@gmail.com>
 *
 *	Permission is hereby granted, free of charge, to any person obtaining a copy
 *	of this software and associated documentation files (the "Software"), to deal
 *	in the Software without restriction, including without limitation the rights
 *	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *	copies of the Software, and to permit persons to whom the Software is
 *	furnished to do so, subject to the following conditions:
 *
 *	The above copyright notice and this permission notice shall be included in
 *	all copies or substantial portions of the Software.
 *
 *	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 *	THE SOFTWARE.
 */
package org.eoti.swing;

import org.eoti.swing.event.FutureActionListener;
import org.eoti.swing.event.FutureMouseListener;
import org.eoti.swing.panels.PaintPanel;
import org.eoti.swing.resources.GUIResourceManager;
import org.eoti.awt.tray.TrayPopup;
import org.eoti.awt.GraphicsUtil;

import javax.swing.*;
import javax.imageio.ImageIO;
import java.awt.event.*;
import java.awt.*;
import java.awt.image.*;
import java.beans.PropertyVetoException;
import java.io.IOException;

public class MDIFrame
extends AFSFrame
implements ActionListener, MouseListener
{
	protected FutureActionListener actionListener;
	protected FutureMouseListener mouseListener;
	protected GUIResourceManager resources;
	protected JDesktopPane desktop;
	protected JMenuBar menuBar;
	protected JToolBar toolBar;
	protected StatusBar statusBar;
	protected TrayPopup tray;
	protected PaintPanel contentPane;

	public MDIFrame(String title)
			throws AWTException
	{
		super(title);
		setup();
	}

	public MDIFrame(String title, GraphicsConfiguration gc)
			throws AWTException
	{
		super(title, gc);
		setup();
	}

	public MDIFrame(String title, String bundle)
			throws AWTException
	{
		this(title, new GUIResourceManager(bundle));
	}

	public MDIFrame(String title, GUIResourceManager resources)
			throws AWTException
	{
		super(title);
		this.resources = resources;
		setup();
	}

	public MDIFrame(String title, GraphicsConfiguration gc, GUIResourceManager resources)
			throws AWTException
	{
		super(title, gc);
		this.resources = resources;
		setup();
	}

	protected void setup()
			throws AWTException
	{
		actionListener = new FutureActionListener(this);
		mouseListener = new FutureMouseListener(this);

		contentPane = createContentPane();
		contentPane.setLayout(new BorderLayout());

		menuBar = createMenuBar();
		setJMenuBar(menuBar);

		toolBar = createToolBar();
		contentPane.add(toolBar, BorderLayout.PAGE_START);

		statusBar = createStatusBar();
		contentPane.add(statusBar, BorderLayout.PAGE_END);

		desktop = createDesktopPane();
		contentPane.add(desktop, BorderLayout.CENTER);

		tray = new TrayPopup(getTitle());

		setContentPane(contentPane);
		maximize();
	}

	protected PaintPanel createContentPane()
	{
		return new PaintPanel();
	}

	protected JDesktopPane createDesktopPane()
	{
		JDesktopPane pane = new JDesktopPane();
		pane.setOpaque(false);
		return pane;
	}

	protected StatusBar createStatusBar()
	{
		return new StatusBar();
	}

	protected JToolBar createToolBar()
	{
		return new JToolBar();
	}

	protected JMenuBar createMenuBar()
	{
		return new JMenuBar();
	}

	public PaintPanel getBackgroundPanel(){return contentPane;}
	public void setResources(GUIResourceManager resources){this.resources = resources;}
	public GUIResourceManager getResources(){return resources;}
	public JToolBar getTools(){return toolBar;}
	public JMenuBar getMenu(){return menuBar;}
	public StatusBar getStatus(){return statusBar;}
	public JDesktopPane getDesktop(){return desktop;}
	public TrayPopup getTray(){return tray;}

	protected void loadActions(String bundleName, String resourceKey)
	{
		resources.loadActions(bundleName, resourceKey, actionListener);
	}

	protected void loadMenus(String bundleName, String resourceKey)
	{
		resources.loadMenus(bundleName, resourceKey, menuBar, mouseListener);
	}

	protected void loadToolbars(String bundleName, String resourceKey)
	{
		resources.loadTools(bundleName, resourceKey, toolBar, mouseListener);
	}

	public void addFrame(JInternalFrame frame)
	{
		frame.setVisible(true);
		desktop.add(frame);
		try{
			frame.setSelected(true);
		}catch(PropertyVetoException pve){

		}
	}

	public void actionPerformed(ActionEvent e)
	{
	}

	public void mouseClicked(MouseEvent e)
	{
	}

	public void mousePressed(MouseEvent e)
	{
	}

	public void mouseReleased(MouseEvent e)
	{
	}

	public void mouseEntered(MouseEvent e)
	{
	}

	public void mouseExited(MouseEvent e)
	{
	}

	protected void setTrayIcon(String resourceKey)
			throws IOException
	{
		String path = resources.getString(resourceKey);
		BufferedImage newImage = ImageIO.read(this.getClass().getResource(path));
		setTrayIcon(newImage);
	}

	protected void setTrayIcon(String imageKey, String colorKey)
			throws IOException
	{
		String path = resources.getString(imageKey);
		BufferedImage transparentImage = ImageIO.read(this.getClass().getResource(path));
		BufferedImage newImage = GraphicsUtil.createImage(transparentImage.getWidth(), transparentImage.getHeight());
		Graphics g = newImage.getGraphics();
		g.setColor(resources.getColor(colorKey));
		g.fillRect(0, 0, newImage.getWidth(), newImage.getHeight());
		g.drawImage(transparentImage, 0, 0, null);
		setTrayIcon(newImage);
	}

	protected void setTrayIcon(BufferedImage image)
			throws IOException
	{
		Image oldImage = getTray().getTrayIcon().getImage();
		getTray().getTrayIcon().setImage(image);
		oldImage.flush();
	}

}
